package com.pcbly.web.persistence.crm;

/**
 * @author carl.song
 * Copyright (c) 2010 Lightsoft, International.
 * All rights reserved.
 *
 */
import java.util.List;

import com.pcbly.web.components.utils.DateHelper;
import com.pcbly.web.components.utils.random.GUID;
import com.pcbly.web.bean.crm.BxdBean;
import com.pcbly.web.persistence.HibernateBaseDao;
import com.pcbly.web.components.hibernate.support.Page;

@SuppressWarnings("unchecked")
public class BxdHibernateDao extends HibernateBaseDao<BxdBean> implements
		BxdDao {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4709945569848437226L;

	protected Class<BxdBean> getEntityClass() {
		return BxdBean.class;
	}

	public BxdHibernateDao() {
		super();
	}

	@Override
	public BxdBean saveBxdBean(BxdBean crm) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug(crm.getId());
		}
		String id = (new GUID()).toString();
		crm.setId(id);
		getHibernateTemplate().save(crm);
		return crm;
	}

	@Override
	public void removeBxdBeanByIds(String ids) throws Exception {
		// TODO Auto-generated method stub
		ids = ids.replaceAll(",", "','");
		String hsql = "update t_crm_bxd set RECYCLED='1' where ID in( '" + ids
				+ "')";
		getSession().createSQLQuery(hsql).executeUpdate();

	}

	@Override
	public void removeBxdBeanById(String id) throws Exception {
		// TODO Auto-generated method stub
		id = id.replaceAll(",", "','");
		String hsql = "update t_crm_bxd set RECYCLED='1' where ID= '" + id
				+ "'";
		getSession().createSQLQuery(hsql).executeUpdate();

	}

	public Page<BxdBean> findBxdBeanByCond(BxdBean bean, Page<BxdBean> page,
			String orderByName, boolean asc) throws Exception {
		StringBuilder sb = new StringBuilder();
		sb.append("select * from t_crm_bxd  ");
//		sb.append(" left join t_crm_bxdproduct p on a.ID=p.BXDID "); 
		sb.append(" where  RECYCLED='0'  ");
		if (!(bean == null)) {
			if (!(bean.getStartDate() == null)) {
				sb.append(" and  JXDATE ");
				sb.append(">='");
				sb.append(DateHelper.dateToStr(bean.getStartDate(),
						"yyyy-MM-dd"));
				sb.append("'");
			}
			if (!(bean.getEndDate() == null)) {
				sb.append(" and  JXDATE ");
				sb.append("<='");
				sb
						.append(DateHelper.dateToStr(bean.getEndDate(),
								"yyyy-MM-dd"));
				sb.append("'");
			}
			if (!(bean.getState() == null || "".equals(bean.getState().trim()))) {
				sb.append(" and  STATE ");
				sb.append(" like '%");
				sb.append(bean.getState());
				sb.append("%'");
			}
			if (!(bean.getGcs() == null || "".equals(bean.getGcs().trim()))) {
				sb.append(" and  GCS ");
				sb.append(" like '%");
				sb.append(bean.getGcs());
				sb.append("%'");
			}
			if (!(bean.getLxr() == null || "".equals(bean.getLxr().trim()))) {
				sb.append(" and  LXR ");
				sb.append(" like '%");
				sb.append(bean.getLxr());
				sb.append("%'");
			}
		}
		if (asc) {
			// criteria.addOrder(Order.asc(orderByName));
			sb.append(" order by  ");
			sb.append(orderByName);
			sb.append(" ");
		} else {
			// criteria.addOrder(Order.desc(orderByName));
			sb.append(" order by   ");
			sb.append(orderByName);
			sb.append(" desc ");
		}
		Object obj = getSession().createSQLQuery(
				"select count(t.ID) from (" + sb + ")t").uniqueResult();
		if (obj == null) {
			obj = "0";
		}

		page.setTotalCount(Integer.parseInt(String.valueOf(obj)));
		if (page.getTotalCount() > 0) {
			int first = page.getFirst();
			int lpage = first + page.getPageSize();
			if (lpage > page.getTotalCount()) {
				lpage = page.getTotalCount();
			}
			List<BxdBean> beans = getSession().createSQLQuery(sb.toString())
					.addEntity(BxdBean.class).list().subList(first, lpage);
			page.setResult(beans);
		}
		return page;
	}
}